<?php

namespace app\admin\controller;

use app\admin\service\MenuService;

class Menu extends CommonController
{
    /**
     * 菜单首页
     * @return
     */
    public function index()
    {
        if ($this->isAjax()) {
            $request = $this->request;
            $list = MenuService::getList($request);
            return $this->success($list);
        }

        return view('menu/index');
    }

    /***
     * 编辑/新增菜单
     * @return
     */
    public function edit()
    {
        $request = $this->request;

        // 保存操作
        if ($this->isAjax()) {
            $formData = $request['form'];
            if (!empty($request['select']))
            {
                $formData['pid'] = $request['select']; // 处理pid
            }
            MenuService::edit($formData);
            return $this->success();
        }

        $menu = [];
        if (!empty($request['id']))
        {
            $menu = MenuService::getMenu($request['id']);
        }
        return view('menu/edit', [
                'pid' => $request['pid'],
                'data' => $menu
            ]
        );
    }

    /**
     * 菜单转成树状结构
     */
    public function tree()
    {
        $request = $this->request;
        $list = MenuService::getTreeList($request);

        return $this->success($list);
    }

    public function del()
    {
        $request = $this->request;
        MenuService::del($request['ids']);

        return $this->success();
    }
}
